<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
        <el-form-item label="机构名称" prop="bankName">
          <el-input
            v-model="queryParams.bankName"
            placeholder="请输入分行/支行名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="摄像头名称" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入摄像头名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        <!-- <el-form-item label="录像机类型" prop="equipmentType">
          <el-select v-model="queryParams.equipmentType" placeholder="请选择录像机类型" clearable>
            <el-option
              v-for="dict in dict.type.equipment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <!-- <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['equipment:config:add']"
          >新增</el-button> -->
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="updateConfig"
            v-hasPermi="['equipment:config:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['equipment:config:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleImport" v-hasPermi="['task:config:import']">导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['equipment:config:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="30" align="center" />
        <el-table-column label="分行名称" align="center" prop="subBankName" />
        <el-table-column label="网点名称" align="center" prop="bankName" />
        <el-table-column prop="equipmentType" label="录像机类型" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.equipment_type" :value="scope.row.equipmentType"/>
          </template>
        </el-table-column>
        <el-table-column label="录像机IP" align="center" prop="equipmentIp" />
        <el-table-column label="通道号" align="center" width="80" prop="equipmentChannel" />
        <el-table-column label="摄像头名称" align="center" prop="remark" />
        <el-table-column label="摄像头对应电脑IP" align="center" prop="cameraComputerIp" />
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column prop="status" label="状态" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleAddChannel(scope.row)"
            >配置通道</el-button> -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="updateConfig(scope.row)"
              v-hasPermi="['equipment:config:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['equipment:config:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改网点摄像头配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="网点号" prop="bankCode">
                <treeselect :disabled="true" v-model="form.bankCode" :options="deptOptions" :normalizer="normalizer" placeholder="选择网点" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="录像机IP" prop="equipmentIp">
                <el-input v-model="form.equipmentIp" :disabled="true" placeholder="请输入录像机ip" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备端口" prop="equipmentPort">
                <el-input v-model="form.equipmentPort" :disabled="true" placeholder="请输入设备端口" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="录像机类型">
                <el-select :disabled="true" v-model="form.equipmentType" placeholder="请选择录像机类型">
                  <el-option v-for="dict in dict.type.equipment_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- <el-row>
            <el-col :span="12">
              <el-form-item label="用户" prop="equipmentUser">
                <el-input v-model="form.equipmentUser" placeholder="请输入设备用户" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="密码" prop="equipmentPwd">
                <el-input v-model="form.equipmentPwd" placeholder="请输入设备密码" type="password" show-password="true"/>
              </el-form-item>
            </el-col>
          </el-row> -->
          <el-row>
            <el-col :span="24">
              <el-form-item label="摄像头名称" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入摄像头名称"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="通道号" prop="equipmentChannel">
                <el-input-number v-model="form.equipmentChannel" controls-position="right" :min="0" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="对应电脑IP" prop="cameraComputerIp">
                <el-input v-model="form.cameraComputerIp" placeholder="对应电脑IP" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
              </el-form-item>
            </el-col>
          </el-row> -->

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- <el-dialog :visible.sync="showDialog" width="600px" title="银行编码" append-to-body>
        <el-table :data="listData" @row-click="handleSelect" highlight-current-row @current-change="handleCurrentChange">
          <el-table-column prop="deptCode" label="网点号" />
          <el-table-column prop="deptName" label="网点名称" />
        </el-table>
      </el-dialog> -->

      <el-dialog title="批量导入任务" :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="fileForm" label-width="100px">
        <el-form-item label="导入模版">
          <el-button size="small" type="primary" @click="downloadTemplate()">下载导入模版</el-button>
        </el-form-item>
        <el-form-item label="请选择文件">
          <el-upload class="upload-demo" ref="upload" accept=".xlsx, .xlsm, .xls" :auto-upload="false" action="#"
            :limit="1" :file-list="fileList" :on-change="handleChange" :on-exceed="handleExceed"
            :on-remove="handleRemove">
            <el-button size="small" type="success">选择文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleImportExcel" :disabled="uploading">确定导入</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </el-dialog>

    </div>
  </template>

<style lang="scss" scoped>
.app-container {
  ::v-deep.vue-treeselect__placeholder {
    font-size: 13px;
  }
  ::v-deep.vue-treeselect__control {
    height: 30px;
  }
}
</style>

  <script>
  import { listConfig, getConfig, delConfigChannel, addConfig, updateConfigChannel ,importConfig} from "@/api/iis/equipment";
  import { listDept } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Config",
    dicts: ['sys_normal_disable','equipment_type'],
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 网点摄像头配置表格数据
        configList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          bankName: "",
          deptId: null,
          bankCode: null,
          bankName: null,
          equipmentIp: null,
          equipmentPort: null,
          equipmentUser: null,
          equipmentPwd: null,
          equipmentType: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          bankCode: [
            { required: true, message: "网点号不能为空", trigger: "blur" }
          ],
          equipmentPort: [
            { required: true, message: "设备端口不能为空", trigger: "blur" }
          ],
          equipmentType: [
            { required: true, message: "录像机类型不能为空", trigger: "blur" }
          ],
          equipmentIp: [
          { required: true, validator: this.validateIP, trigger: 'blur' }
          ]
        },
        showDialog: false,
        listData: [],
        // 机构树选项
        deptOptions: [],
        deptqueryParams: {
          deptCode: undefined
        },
        //excel批量导入功能
        dialogFormVisible: false,
        fileList: [],  //文件列表
        file: {},
        uploading: true //默认置灰确定导入按钮
      };
    },
    created() {
      this.getList();
      listDept().then(response => {
          this.deptOptions = this.handleTree(response.data, "deptId");
      });
    },
    methods: {
      /** 查询网点摄像头配置列表 */
      getList() {
        this.loading = true;
        listConfig(this.queryParams).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          equipmentId: null,
          subBankCode: null,
          deptId: null,
          bankCode: null,
          bankName: null,
          equipmentIp: null,
          equipmentPort: null,
          equipmentUser: null,
          equipmentPwd: null,
          equipmentType: '01',
          status: '0',
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.channelId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加网点摄像头配置";
      },
      /** 修改按钮操作 */
      updateConfig(row) {
        this.reset();
        const channelId = row.channelId || this.ids
        getConfig(channelId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改网点摄像头配置";
        });
      },
      /** 增加录像机通道 */
      handleAddChannel(row) {
        const equipment_id = row.equipmentId || 0;
        this.$router.push('/iis/equipment-channel/index/' + equipment_id)
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.channelId != null) {
              updateConfigChannel(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
            //  else {
            //   addConfig(this.form).then(response => {
            //     this.$modal.msgSuccess("新增成功");
            //     this.open = false;
            //     this.getList();
            //   });
            // }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const channelIds = row.channelId || this.ids;
        this.$modal.confirm('是否确认删除通道配置编号为"' + channelIds + '"的数据项？').then(function () {
          return delConfigChannel(channelIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('equipment/config/export', {
          ...this.queryParams
        }, `分行摄像头列表.xlsx`)
      },
      handleSelect(row) {
        this.$refs.form.clearValidate(['bankCode']);
        this.form.bankCode = row.deptCode;
        this.form.bankName = row.deptName;
        this.form.deptId = row.deptId;
        this.showDialog = false;
      //this.$forceUpdate();
      },
      handleCurrentChange(currentRow) {
        this.selectedRow = currentRow;
      },
      getBranchInfo() {
        listDept(this.deptqueryParams).then(response => {
            console.log(response.data);
            this.listData = response.data;
        });
        this.showDialog = true;
      },
      /** 转换部门数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptCode,
          label: node.deptName,
          children: node.children
        };
      },
      validateIP(rule, value, callback) {
        const ipReg = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
        if (!value) {
          callback(new Error('IP 地址不能为空'));
        } else if (!ipReg.test(value)) {
          callback(new Error('请输入合法的 IPv4 地址'));
        } else {
          callback();
        }
      },
       /** 批量导入操作 */
      handleImport() {
        this.dialogFormVisible = true;
        this.fileList = [];
        this.file = {};
        this.uploading = true; // 置灰确定导入按钮
      },
      //excel批量打入功能
      handleChange(file) {     //(file,fileList)参数
        this.uploading = false;
        this.file = file;
      },
      //文件超出个数限制
      handleExceed() {
        this.$message.warning("仅允许上传一个文件！");
      },
      //导入excel
      handleImportExcel() {
        if (!this.file.size) {
          this.$message.warning("请选择上传的文件");
          return;
        }
        this.uploading = true;
        let formData = new FormData();
        formData.append("file", this.file.raw);
        importConfig(formData).then(res => {
          this.$modal.msgSuccess("导入成功");
          this.uploading = false;  //调用接口
          this.dialogFormVisible = false;
          this.getList();
        });
      },
      downloadTemplate() {
        this.download('equipment/config/downloadTemplate', {}, `templateEquipment.xlsx`)
      },
       //取消
      handleCancel() {
        this.file = {};
        this.fileList = [];
        this.dialogFormVisible = false;
        this.uploading = true;
      },
      //移除文件
      handleRemove() {
        this.uploading = true;
      },
    }
  };
  </script>
